import { _decorator, CCFloat, CCInteger, Component, Label, tween, UIOpacity } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('ScoreEffect')
export class ScoreEffect extends Component {

    private label: Label = null;

    @property(CCFloat)
    riseDuration: number = 1.0;

    @property(CCInteger)
    riseDistance: number = 50;

    @property(CCFloat)
    fadeDuration: number = 1.0;

    onLoad() {
        if (!this.label) {
            this.label = this.getComponent(Label);
        }
    }

    showScore(value: number) {
        this.label.string = "+" + value.toString();

        // 初始化位置和透明度
        let opacityComp = this.getComponent(UIOpacity)
        opacityComp.opacity = 255;
        this.node.getPosition().y = 0

        // 执行上升和淡出动画
        tween(this.node)
            .parallel(
                tween().by(this.riseDuration, { y: this.riseDistance }, { easing: 'sineOut' }),
                tween().to(this.fadeDuration, { opacity: 0 }, { easing: 'sineIn' })
            )
            .call(() => {
                this.node.destroy();
            })
            .start();
    }

}